package cn.cheln.explorer.beans;

import java.io.File;
import java.util.Date;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import cn.cheln.explorer.config.Config;
import cn.cheln.explorer.ds.sdcard.TypeFilter;
import cn.cheln.support.format.HourFormatter;
import cn.power.shine.R;

import com.direction.hold.VPAty;

public class FileInfo {

	public static TypeFilter fileFetcher = new TypeFilter(new Integer[] {
			TypeFilter.FILTER_BOTH_DIR_FILE,
			Config.getHiddenOption(Config.SDCARD_HIDE_OPTION) }, null, null);
	public static final int DOC_ICON = R.drawable.doc;
	public static final int IMAGE_LOADING_ICON = R.drawable.lewa_ic_loading_picture;
	public static final int VIDEO_ICON = R.drawable.lewa_ic_folder_video;
	public static final int PACKAGE_ICON = R.drawable.lewa_ic_folder_package_setup;
	public static final int AUDIO_ICON = R.drawable.lewa_ic_folder_music_mp3;
	public static final int THEME_ICON = -2;
	public Boolean checkboxOption = false;
	public final static int DIR_RES = R.drawable.ic_launcher_folder;
	public final static int OTHER_FILE_RES = R.drawable.lewa_ic_other_folder;
	public static Bitmap iconVideo;
	public static Bitmap iconPackage;
	public static Bitmap iconDir;
	public static Bitmap iconOtherFile;
	public static Bitmap iconDoc;
	public static Bitmap iconAudio;
	public static Bitmap iconImageLoading;
	public static Bitmap iconTheme;
	public String sortKey;
	public String path;
	public String name;
	public String sizeText;
	public Long leng;
	public String type;
	public String lastModified;
	public Boolean isDir;
	public Integer count;
	public String countStr;
	public Object iconRes = -1;
	public Long lastModifiedInt;
	public Date unformattedDate;
	public Boolean overrideFlag;
	public Boolean isCutNew;
	public boolean isFileBuilt;
	public boolean isIconBinded;
	public boolean isUISelected;
	private Context context;
	private boolean forceDir;
	public Integer fileformat;

    public String thumbnailPath;
    public long dbId;
	public boolean isForceDir() {
		return forceDir;
	}

	public void setForceDir(boolean forceDir) {
		this.forceDir = forceDir;
	}

	public static void init(Context context) {
		if (iconDir == null) {
			iconDir = ((BitmapDrawable)context.getResources().getDrawable(DIR_RES)).getBitmap();
		}
		if (iconOtherFile == null) {
			iconOtherFile = ((BitmapDrawable)context.getResources().getDrawable(OTHER_FILE_RES)).getBitmap();
		}
		if (iconDoc == null) {
			iconDoc = ((BitmapDrawable)context.getResources().getDrawable(DOC_ICON)).getBitmap();
		}
		if (iconImageLoading == null) {
			iconImageLoading = ((BitmapDrawable)context.getResources().getDrawable(
					IMAGE_LOADING_ICON)).getBitmap();
		}
		if (iconVideo == null) {
			iconVideo = ((BitmapDrawable)context.getResources().getDrawable(VIDEO_ICON)).getBitmap();
		}
		if (iconAudio == null) {
			iconAudio = ((BitmapDrawable)context.getResources().getDrawable(AUDIO_ICON)).getBitmap();
		}
		if (iconPackage == null) {
			iconPackage = ((BitmapDrawable)context.getResources().getDrawable(PACKAGE_ICON)).getBitmap();
		}
//		if (iconTheme == null) {
//			iconTheme = context.getResources().getDrawable(THEME_ICON);
//		}
	}

	public Integer getCount() {
		// return (Integer) get("count");
		return count;
	}

	public void setCount(Integer count) {
		// this.put("count", count);
		this.count = count;
	}

	public Boolean getIsDir() {
		// return (Boolean) this.get("isDir");
		return isDir;
	}

	public void setIsDir(Boolean isDir) {
		// this.put("isDir", isDir);
		this.isDir = isDir;
	}

	public Long getLastModifiedInt() {
		// return (Long) this.get("lastModifiedInt");
		return this.lastModifiedInt;
	}

	public void setLastModifiedInt(Long lastModifiedInt) {
		// this.put("lastModifiedInt", lastModifiedInt);
		this.lastModifiedInt = lastModifiedInt;
	}

	public Long getLeng() {
		// return (Long) this.get("leng");
		return this.leng;
	}

	public void setLeng(Long leng) {
		// put("leng", leng);
		this.leng = leng;
	}

	public String getName() {
		// return (String) this.get("name");
		return this.name;
	}

	public void setName(String name) {
		// put("name", name);
		this.name = name;
	}

	public String getPath() {
		// return (String) this.get("path");
		return this.path;
	}

	public void setPath(String path) {
		// put("path", path);
		this.path = path;
	}

	public String getLastModified() {
		// return (String) get("lastModified");
		return this.lastModified;
	}

	public void setLastModified(String lastModified) {
		// put("lastModified", lastModified);
		this.lastModified = lastModified;
	}

	public String getCountStr() {
		// return (String) get("countStr");
		return this.countStr;
	}

	public void setCountStr(String countStr) {
		// put("countStr", countStr);
		this.countStr = countStr;
	}

	public Object getIconRes() {
		// return get("iconRes");
		return this.iconRes;
	}

	public void setIconRes(Object iconRes) {
		// put("iconRes", iconRes);
		this.iconRes = iconRes;
	}

	public String getSizeText() {
		// return (String) get("sizeText");
		return this.sizeText;
	}

	public void setSizeText(String sizeText) {
		// put("sizeText", sizeText);
		this.sizeText = sizeText;
	}

	public FileInfo(File file, Context context) {
		this.context = context;
		buildFile(file);
		isFileBuilt = true;
	}

	public FileInfo(File parent, String name, Context context) {
		path = parent.getAbsolutePath() + "/" + name;
		this.context = context;
	}

	public void buildCountStr() {
		count = this.getCount();
		countStr = count == -1 ? "" : "  ( "
				+ (count == -2 ? "..." : count + "") + " )";
		// put("countStr", countStr);
	}

	public String getShowName() {
		return name;
	}

	public String getType() {
		// return this.get("type").toString();
		return this.type;
	}

	public void buildFile() {
		if (!isFileBuilt) {
			buildFile(new File(path));
			isFileBuilt = true;
		}
	}

	public void buildName() {
		// Object objName = this.get("name");
		// if (objName != null) {
		// this.put("name", FileUtil.getName(get("path").toString()));
		// }
		name = path == null ? null : FileUtil.getName(path);
	}

	public void setType(String type) {
		this.type = type;
	}

	public void buildExtension() {
		// Object objType = this.get("type");
		// if (objType != null) {
		// this.put("type",
		// FileUtil.getRealExtension(this.getName()).toString());
		// }
		type = type == null ? null : FileUtil.getRealExtension(this.getName());
	}

	public static FileInfo hasFileInfo(Class<? extends FileInfo> clazz,
			File file, Context context) {
		return hasFileInfo(clazz, file, context, null);
	}

	public static FileInfo hasFileInfoOnEntity(FileInfo item, File file,
			Context context) {
		if (!(item instanceof FileInfo)) {
			throw new IllegalStateException("wrong type info");
		}
		return hasFileInfo(item.getClass(), file, context, null);
	}

	public static FileInfo hasFileInfo(Class<? extends FileInfo> clazz,
			File file, Context context, Boolean duplicatedFlag) {
		FileInfo fileinfo = null;
		try {
			if (duplicatedFlag == null) {
				fileinfo = clazz.getConstructor(File.class, Context.class)
						.newInstance(file, context);
			} else {
				fileinfo = clazz.getConstructor(File.class, Context.class,
						Boolean.class).newInstance(file, context,
						duplicatedFlag);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return fileinfo;
	}

	public void buildFile(File file) throws NumberFormatException {
		if(!file.exists()){
			isDir = true;
		}else{
			isDir = forceDir?true:file.isDirectory();
		}
		path = file.getAbsolutePath();
		name = file.getName();
		type = FileUtil.getRealExtension(name);
		leng = file.length();
		sizeText = FileUtil.formatSize(leng);
		if (this.isDir) {
			sizeText = "";
		}
		lastModifiedInt = file.lastModified();
		lastModified = HourFormatter.showFormattedDate(
				new Date(lastModifiedInt), (Context) (context==null?VPAty.activity:context));
		count = isDir ? -2 : -1;
		iconRes = -1;
		buildCountStr();
	}

	public FileInfo() {
	}

	public FileInfo(String path) {
		this.path = path;
	}
    public FileInfo(String path,Context context) {
        this(new File(path),context);

    }
	public File getFile() {
		return this.getPath() == null ? null : new File(this.getPath());
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == null) {
			return false;
		}
		final FileInfo other = (FileInfo) obj;
		if ((this.getPath() == null) ? (other.getPath() != null) : !this
				.getPath().equals(other.getPath())) {
			return false;
		}
		return true;
	}

	public FileInfo(File file, Context context, Boolean duplicatedFlag) {
		this(file, context);
		overrideFlag = duplicatedFlag;
	}
}
